﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NuclexVectorFontContent = Nuclex.Fonts.Content.VectorFontContent;
using NuclexVectorFontCharacterContent = Nuclex.Fonts.Content.VectorFontCharacterContent;
using System.Reflection;

namespace Qreed.Xna.Font.ContentPipeline
{
    public class VectorFontContent
    {
        /// <summary>
        /// Converts to <see cref="VectorFont"/>.
        /// </summary>
        /// <returns></returns>
        internal VectorFont ConvertToVectorFont()
        {
            VectorFont font = new VectorFont();

            SetVectorFontProperty(font, "LineHeight", this.NuclexVectorFontContent.LineHeight);

            return font;
        }

        private void SetVectorFontProperty(VectorFont font, string propertyName, object value)
        {
            Type t = typeof(VectorFont);
            PropertyInfo pi = t.GetProperty(propertyName);
            pi.SetValue(font, value, null);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VectorFontContent"/> class.
        /// </summary>
        /// <param name="nuclexVectorFontContent">Content of the nuclex vector font.</param>
        internal VectorFontContent(NuclexVectorFontContent nuclexVectorFontContent)
        {
            if (nuclexVectorFontContent == null)
                throw (new ArgumentNullException("nuclexVectorFontContent"));

            _nuclexVectorFontContent = nuclexVectorFontContent;
        }

        private string _fontName;
        /// <summary>
        /// Gets or sets the name of the font.
        /// </summary>
        /// <value>The name of the font.</value>
        public string FontName
        {
            get { return this._fontName; }
            set { this._fontName = value; }
        }			

        private NuclexVectorFontContent _nuclexVectorFontContent;
        /// <summary>
        /// Gets the content of the nuclex vector font.
        /// </summary>
        /// <value>The content of the nuclex vector font.</value>
        internal NuclexVectorFontContent NuclexVectorFontContent
        {
            get { return this._nuclexVectorFontContent; }
        }

        private Dictionary<char, VectorFontCharacterContent> _characterMap = new Dictionary<char, VectorFontCharacterContent>();
        /// <summary>
        /// Gets the character map.
        /// </summary>
        /// <value>The character map.</value>
        public Dictionary<char, VectorFontCharacterContent> CharacterMap
        {
            get { return this._characterMap; }
        }

        internal void Process()
        {
            foreach (char k in NuclexVectorFontContent.CharacterMap.Keys)
            {
                int index = NuclexVectorFontContent.CharacterMap[k];

                VectorFontCharacterContent charContent = new VectorFontCharacterContent(k, NuclexVectorFontContent.Characters[index]);
                charContent.Process();

                CharacterMap.Add(k, charContent);
            }
        }
    }
}
